/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package fr.castorflex.ohos.verticalviewpager.transformer;

import ohos.agp.components.Component;

/**
 * 页面缩放动画接口
 *
 * @author stt
 * @since 2021-04-26
 */
public class ZoomOutSlideTransformer extends BaseTransformer {
    private static final float ALPHA = 0.14999998F;
    private static final float ALPHA_MAX = 0.85F;
    private static final float ALPHA_DEFAULT = 0.7F;
    private static final float SCALE_MAX = 0.85F;
    private static final float PIVOT_X = 0.5F;
    private static final float PIVOT_Y = 0.5F;
    private static final float HALF = 2.0F;
    private static final float SCROLL_RIGHT = -1.0F;
    private static final float SCROLL_LEFT = 1.0F;

    @Override
    protected void onTransform(Component component, float position) {
        if (position >= SCROLL_RIGHT || position <= SCROLL_LEFT) {
            float height = (float) component.getHeight();
            float width = (float) component.getWidth();
            float scaleFactor = Math.max(SCALE_MAX, MathUtils.floatToSubtract(SCROLL_LEFT, Math.abs(position)));
            float vertMargin = height * MathUtils.floatToSubtract(SCROLL_LEFT, scaleFactor) / HALF;
            float horzMargin = width * MathUtils.floatToSubtract(SCROLL_LEFT, scaleFactor) / HALF;
            component.setPivotY(PIVOT_X * height);
            component.setPivotX(PIVOT_Y * width);
            component.setTranslationX(position < (float) 0
                    ? MathUtils.floatToSubtract(horzMargin, vertMargin / HALF)
                    : MathUtils.floatToAdd(-horzMargin, vertMargin / HALF));
            component.setScaleX(scaleFactor);
            component.setScaleY(scaleFactor);
            component.setAlpha(MathUtils.floatToAdd(ALPHA_DEFAULT,
                    MathUtils.floatToSubtract(scaleFactor, ALPHA_MAX) / ALPHA / HALF));
        }
    }
}
